package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.DeviceInfo;
import com.example.prison.service.DeviceInfoService;
import com.example.prison.mapper.DeviceInfoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jiang
* @description 针对表【device_info】的数据库操作Service实现
* @createDate 2023-05-29 11:38:26
*/
@Service
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo>
    implements DeviceInfoService{

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Override
    public PageInfo<DeviceInfo> get(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(deviceInfoMapper.get());
    }

    @Override
    public int add(DeviceInfo device) {
        String deviceNumber = device.getDeviceNumber();
        if (ObjectUtils.isNotEmpty(deviceInfoMapper.getOneByNumber(deviceNumber))) {
            return -1;
        } else {
            deviceInfoMapper.add(device);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
        deviceInfoMapper.deleteDevice(ids);
    }

    @Override
    public int update(DeviceInfo device, Integer deviceId) {
        DeviceInfo deviceDTO = deviceInfoMapper.getById(deviceId);
        deviceInfoMapper.updateDevice(device, deviceId);
        List<DeviceInfo> deviceList = deviceInfoMapper.getByNumber(device.getDeviceNumber());
        if(deviceList.size() > 1) {
            deviceInfoMapper.updateDeviceFail(deviceDTO, deviceId);
            return -1;
        } else {
            return 1;
        }
    }

    @Override
    public DeviceInfo getById(Integer deviceId) {
        return deviceInfoMapper.getById(deviceId);
    }

    @Override
    public PageInfo<DeviceInfo> getByName(int pageNumber, int pageSize, String deviceName) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(deviceInfoMapper.getByName(deviceName));
    }

    @Override
    public DeviceInfo getOneByName(String deviceName) {
        return deviceInfoMapper.getOneByName(deviceName);
    }

    @Override
    public PageInfo<DeviceInfo> getByNumber(int pageNumber, int pageSize, String deviceNumber) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(deviceInfoMapper.getByNumber(deviceNumber));
    }

    @Override
    public DeviceInfo getOneByNumber(String deviceNumber) {
        return deviceInfoMapper.getOneByNumber(deviceNumber);
    }

}




